	const db = uniCloud.database()
	const dbCmd = db.command // 取指令
	module.exports = {

		//首页商品列表
		async getAllGoods(lastDate) {

			if (lastDate == null) {
				let res = await db.collection('opendb-mall-goods').where({
						is_on_sale: true
					}).orderBy('auto_createdate', 'asc')
					.limit(
						5).get()
				return res.data;
			}

			let res = await db.collection('opendb-mall-goods').where({
				auto_createdate: dbCmd.gt(lastDate),
				is_on_sale: true
			}).orderBy('auto_createdate', 'asc').limit(5).get()

			// 返回结果  是一个对象数组
			return res.data;
		},


		async getSort() {
			let res = await db.collection('opendb-mall-categories').where({
				'is_on_sale': true
			}).field({
				'name': true
			}).get()

			// 返回结果  是一个对象数组
			return res.data;
		},


		//获取分类列表
		async getByParentsort() {
			let res = await db.collection("opendb-mall-categories").field({
				'name': true
			}).get()

			// 返回结果  是一个对象数组
			return res.data;
		},


		//获取分类栏商品列表
		async tabChanged(cate, lastDate) {
			let res

			res = await db.collection('opendb-mall-goods').where({
				'is_on_sale': true
			}).orderBy('auto_createdate', 'asc').where({
				category_name: cate
			}).get()
			return res.data
		},

		async getById(id) {
			let res = await db.collection('opendb-mall-goods').where({
				'_id': id
			}).get()
			return res.data
		},

		// 获取设置的活动时间
		async getCountdown() {
			let res = await db.collection('activity-settings').limit(1).field({
				'endDate': true
			}).get()

			let end = res.data[0].endDate
			let now = new Date().getTime()
			let Dvalue = end - now

			let nowdate = new Date(now)
			let enddate = new Date(end)
			let re = nowdate.getDay() + ":" + nowdate.getHours() + ":" + nowdate.getMinutes() + ":" + nowdate
				.getSeconds()
			let day = Math.floor(Dvalue / 1000 / 60 / 60 / 24)
			let hour = Math.floor((Dvalue / 1000 / 60 / 60 / 24 - day) * 24)
			let minute;
			let sencond;
			if (hour > 24) {
				minute = Math.floor(((Dvalue / 1000 / 60 / 60 / 24 - day) * 24 - hour) * 60) + (hour - 24) * 60
				hour = 24
			} else {
				minute = Math.floor(((Dvalue / 1000 / 60 / 60 / 24 - day) * 24 - hour) * 60)
			}

			if (minute > 60) {
				sencond = Math.floor((((Dvalue / 1000 / 60 / 60 / 24 - day) * 24 - hour) * 60 - minute) * 60) + (
					minute -
					60) * 60
				minute = 60
			} else {
				sencond = Math.floor((((Dvalue / 1000 / 60 / 60 / 24 - day) * 24 - hour) * 60 - minute) * 60)
			}

			return {
				'day': day,
				'hour': hour,
				'minute': minute,
				'second': sencond
			}
		},
		async getOrderGoods(arr) {
			let res = await db.collection('opendb-mall-goods').where({
				'_id': dbCmd.in(arr)
			}).get()

			return res.data
		},

		async getGoodsListByIds(ids) {
			let res = await db.collection('opendb-mall-goods').where({
				'_id': dbCmd.in(ids)
			}).get()

			return res.data
		},

	}